<template>
  <div class="order">
    <h1>订单管理</h1>
    <div class="play">
      <el-row :gutter="10">
        <el-col :span="4">
          <el-select v-model="value">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-input v-model="input" placeholder="关键词"></el-input>
        </el-col>
        <el-col :span="2">
          <el-button plain @click="search()">查询</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="goodList">
      <el-table :data="tableData" border style="width: 100%" max-height="700">
        <el-table-column fixed prop="orderNo" label="订单号" width="auto">
        </el-table-column>
        <el-table-column prop="receiverName" label="收件人" width="200">
        </el-table-column>
        <el-table-column prop="statusDesc" label="订单状态" width="200">
        </el-table-column>
        <el-table-column prop="payment" label="订单总价" width="200">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="auto">
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pageData.pageNum"
          :page-size="pageData.pageSize"
          layout="prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { order, orderSearch } from "../api/index";
export default {
  name: "order",
  data() {
    return {
      value: 0,
      options: [
        {
          value: 0,
          label: "按订单号查询",
        },
      ],
      input: "",
      currentPage3: 5,
      tableData: [],
      pageData: {
        pageSize: 10,
        pageNum: 1,
      },
      total: 0,
    };
  },
  created() {
    this.init(this.pageData);
  },
  methods: {
    init(val) {
      order({ pageNum: val.pageNum, pageSize: val.pageSize }).then((res) => {
        console.log(res);
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.pageData.pageNum = res.data.pageNum;
        this.pageData.pageSize = res.data.pageSize;
      });
    },
    search() {
      if (this.input) {
        orderSearch({
          ...this.pageData,
          orderNo: this.input,
        }).then((res) => {
          console.log(res);
          if (res.data) {
            this.tableData = res.data.list;
            this.total = res.data.total;
            this.pageData.pageNum = res.data.pageNum;
            this.pageData.pageSize = res.data.pageSize;
          } else {
            this.$message.error("此订单不存在");
          }
        });
        this.input = ""
      } else {
        this.init(this.pageData);
      }
    },
    handleClick(row) {
      console.log(row);
      this.$router.push({
        path:"/order/detail",
        query:{
          orderNo:row.orderNo
        }
      })
    },
    handleSizeChange(val) {
      this.pageData.pageSize = val;
      this.init(this.pageData);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.pageData.pageNum = val;
      this.init(this.pageData);
      console.log(`当前页: ${val}`);
    },
  },
};
</script>

<style scoped lang="scss">
.play{
  margin: 20px 0;
}
</style>